//
//  AccountView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/28.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class AccountView: UIView {
    
    // MARK: 赋值
    public var accountBeans:[AccountBean]? {
        didSet {
            updateLayout()
        }
    }
    // MARK: 回调
    public var complete:((_ item:Int) -> Void)?
    public var completeBackground:((_ account:AccountBean) -> Void)?
    
    // scrollView
    private let scrollView = UIScrollView()
    
    override func layoutSubviews() {
        guard let accounts = accountBeans else {
            return
        }
        // 高度
        var tempHeight:CGFloat = 0
        for i in 0..<accounts.count {
            // 背景颜色
            let backround = UIButton()
            backround.tag = i
            // 姓名
            let usernameView = UILabel(title: "测试", color: UIColor.black, size: 18)
            usernameView.isUserInteractionEnabled = false
            usernameView.text = accounts[i].username
            usernameView.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN140, height: MARGIN20)
            backround.addSubview(usernameView)
            // 编辑图片
            let editView = UIButton()
            editView.tag = i
            let editX = frame.width - MARGIN50
            editView.setImage(UIImage(named: "edit_15x15"), for: .normal)
            editView.frame = CGRect(x: editX, y: 0, width: MARGIN50, height: MARGIN50)
            backround.addSubview(editView)
            // 编辑图片 点击事件
            editView.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            // 手机号
            let mobileY = usernameView.frame.maxY + MARGIN20
            let mobileView = UILabel(title: "手机号", color: UIColor.gray, size: 13)
            mobileView.isUserInteractionEnabled = false
            mobileView.frame = CGRect(x: MARGIN, y: mobileY, width: MARGIN70, height: MARGIN)
            backround.addSubview(mobileView)
            // 手机号文本
            let mobileTextView = UILabel(title: "18612523200", color: COLOR_NICE, size: 13)
            mobileTextView.isUserInteractionEnabled = false
            mobileTextView.text = accounts[i].mobile
            mobileTextView.frame = CGRect(x: mobileView.frame.maxX, y: mobileY, width: MARGIN140, height: MARGIN)
            backround.addSubview(mobileTextView)
            // 邮箱地址
            let emailY = mobileView.frame.maxY + MARGIN10
            let emailView = UILabel(title: "邮箱地址", color: UIColor.gray, size: 13)
            emailView.isUserInteractionEnabled = false
            emailView.frame = CGRect(x: MARGIN, y: emailY, width: MARGIN70, height: MARGIN)
            backround.addSubview(emailView)
            // 邮箱地址文本
            let emailTextView = UILabel(title: "wanghz@gmrz-bj.com", color: COLOR_NICE, size: 13)
            emailTextView.isUserInteractionEnabled = false
            emailTextView.text = accounts[i].email
            let emialW = frame.width - emailView.frame.maxX - MARGIN
            emailTextView.frame = CGRect(x: emailView.frame.maxX, y: emailY, width: emialW, height: MARGIN)
            backround.addSubview(emailTextView)
            // 下划线
            let line = UIView()
            if i != accounts.count - 1 {
                line.backgroundColor = COLOR_LINE
            }
            let lineY = emailView.frame.maxY + MARGIN20
            line.frame = CGRect(x: MARGIN, y: lineY, width: frame.width - MARGIN * 2, height: 1)
            backround.addSubview(line)
            // 背景颜色
            backround.backgroundColor = UIColor.white
            backround.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: line.frame.maxY)
            scrollView.addSubview(backround)
            // 每条高度
            tempHeight += line.frame.maxY
            // 添加点击事件
            backround.addTarget(self, action: #selector(clickbackgroundItem(btn:)), for: .touchUpInside)
        }
        addSubview(scrollView)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollView.contentSize = CGSize(width: frame.width, height: tempHeight + MARGIN200)
        
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let callback = complete {
            callback(btn.tag)
        }
    }
    
    @objc func clickbackgroundItem(btn:UIButton) -> Void {
        if let callback = completeBackground ,let bean = accountBeans{
            let bean = bean[btn.tag]
            callback(bean)
        }
    }
}
